package erp.education.server.beans.implementation.finance.master;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import erp.education.client.finance.master.FeeHeadRequest;
import erp.education.server.beans.implementation.BaseService;
import erp.education.server.beans.implementation.ResultResponse;
import erp.education.server.beans.implementation.ServiceObject;
import erp.education.server.beans.interfaces.finance.master.FeeStructureServiceI;
import erp.education.server.beans.interfaces.user.transaction.ServiceObjectI;
import erp.education.server.db.entites.finance.FeeHeads;
import erp.education.server.db.exceptions.DbExceptions;

@Component("feeStructureService")
public class FeeStructureService extends BaseService implements
		FeeStructureServiceI {

	@Override
	public ServiceObjectI feeHeadLoadSevice(FeeHeadRequest feeHeadRequest)
			throws DbExceptions {
		ServiceObjectI response = new ServiceObject();
		ResultResponse holder = new ResultResponse();
		List result = new ArrayList();
		result.add(feeGroupDao.getFeeGroups());
		result.add(feeHeadDao.getFeeHeads());
		holder.setResultList(result);
		response.setResponseObject(holder);
		return response;
	}

	@Override
	public ServiceObjectI saveUpdateFeeHeadDetails(FeeHeadRequest feeHeadRequest)
			throws DbExceptions {
		ServiceObjectI response = new ServiceObject();
		ResultResponse holder = new ResultResponse();
		List result = new ArrayList();
		FeeHeads feeHead = null;

		feeHead = feeHeadDao.getFeeHead(feeHeadRequest.getFeeCode());

		if (null == feeHead) {
			feeHead = new FeeHeads();

		}
		if (null == feeHeadRequest.getTaxExemption()
				|| "Y".equals(feeHeadRequest.getTaxExemption())) {
			feeHeadRequest.setTaxExemption("N");
		} else {
			feeHeadRequest.setTaxExemption("Y");

		}
		if (null == feeHeadRequest.getRefundable()
				|| "Y".equals(feeHeadRequest.getRefundable())) {
			feeHeadRequest.setRefundable("N");
		} else {
			feeHeadRequest.setRefundable("Y");

		}
		feeHead.setFeeType(feeHeadRequest.getFeeHeadType());
		feeHead.setFeeGroupId(feeHeadRequest.getGroupId());
		feeHead.setFeeName(feeHeadRequest.getFeeHeadName());
		feeHead.setFeeRefundable(feeHeadRequest.getRefundable());
		feeHead.setTaxExempted(feeHeadRequest.getTaxExemption());
		feeHeadDao.merge(feeHead);
		result.add(schoolDao.getSchools());
		result.add(branchsDao.getBranchList());
		holder.setResultList(result);
		response.setResponseObject(holder);
		return response;
	}
}
